//
//  TodayNewsViewController.m
//  口袋
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "TodayNewsViewController.h"
#import "TodayNewsTopView.h"
#import "TodayNewsTableViewCell.h"
#import "SAReachabilityManager.h"
#import "TodayNewsModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SANetWorkingTask.h"
#import "TodaySubjectViewController.h"
#import "ShowImageViewController.h"
#import "NewsDetailViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "GFPlaceholderView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface TodayNewsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) GFPlaceholderView *placeHolder;
@end

@implementation TodayNewsViewController
- (void)dealloc
{
    [_placeHolder release];
    [_array release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        
    }
    return self;
}

#pragma mark -
#pragma mark 数据请求
- (void)startConnect
{
    [SANetWorkingTask request:@"http://c.3g.163.com/nc/article/list/T1429173683626/0-20.html" parmater:nil block:^(NSDictionary *result) {
        NSArray *arr = [result objectForKey:@"T1429173683626"];
        for (NSDictionary *dic in arr) {
            TodayNewsModel *model = [[TodayNewsModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            [model release];
        }
        TodayNewsTopView *topView = [[TodayNewsTopView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width / 1.23)];
        TodayNewsModel *model = [self.array objectAtIndex:0];
        [topView.imageView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        topView.target = self;
        topView.action = @selector(topViewAction:);
        topView.titleLabel.text = model.title;
        topView.contentLabel.text = model.digest;
        
        NSNumber * a = model.replyCount;
        NSInteger b = [a integerValue];
        if (b > 10000) {
            CGFloat count = b * 1.0 / 10000 * 1.0;
            [topView.commentButton setTitle:[NSString stringWithFormat:@"  %.2f万跟帖", count] forState:UIControlStateNormal];
        }else{
            [topView.commentButton setTitle:[NSString stringWithFormat:@"  %@跟帖", model.replyCount] forState:UIControlStateNormal];
        }
        
        [self.table setTableHeaderView:topView];
        [topView release];

        [self.table reloadData];
    }];
}
- (void)loadView
{
    [super loadView];
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UIImageView *today = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 130, 41)];
    [today setImage:[UIImage imageNamed:@"hotnews.png"]];
    self.navigationItem.titleView = today;
    [today release];
    //self.navigationController.hidesBarsOnSwipe=YES;
    //将返回按钮的文字position设置不在屏幕上显示
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    
    self.placeHolder = [[GFPlaceholderView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49)];
    [self.placeHolder showLoadingView];
    [self.view addSubview:self.placeHolder];
    [self.placeHolder release];
    
    [self startConnect];
    [self createTableView];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
}
- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = HEIGHT*0.15;
     [self.table setSeparatorColor:[UIColor colorWithRed:128/255.0 green:217/255.0 blue:215/255.0 alpha:1.0]];
    //上拉刷新
    [_table addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [_table.header beginRefreshing];
    //下拉加载
    [_table addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    //_a = 0;
    _table.footer.automaticallyRefresh = NO;
    [self.view addSubview:self.table];
    [self.table release];
//    [topView release];
    
}

- (void)loadMoreData
{
    UIAlertView *pice = [[UIAlertView alloc]initWithTitle:@"口袋提示" message:@"今天的要闻就这么多哟~~\(≧▽≦)/~" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    
    [pice show];
    [pice release];
    [self.table.footer endRefreshing];
}

- (void)loadNewData
{
    [_table reloadData];
    [_table.header endRefreshing];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count - 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TodayNewsModel *model = [self.array objectAtIndex:indexPath.row+1];
    TodayNewsTableViewCell *cell = [[[TodayNewsTableViewCell alloc]init]autorelease];
    if (indexPath.row <= 1) {
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        cell.titleLabel.text = model.title;
        cell.contentLabel.text = model.digest;
        NSNumber * a = model.replyCount;
        NSInteger b = [a integerValue];
        if (b > 10000) {
            CGFloat count = b * 1.0 / 10000 * 1.0;
            [cell.commentButton setTitle:[NSString stringWithFormat:@"  %.1f万跟帖", count] forState:UIControlStateNormal];
        }else{
            [cell.commentButton setTitle:[NSString stringWithFormat:@"  %@跟帖", model.replyCount] forState:UIControlStateNormal];
        }
        if (indexPath.row == 0) {
            UIImageView *topicon2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
            [topicon2 setImage:[UIImage imageNamed:@"top2.png"]];
            [cell addSubview:topicon2];
            [topicon2 release];
        }else if (indexPath.row == 1){
            UIImageView *topicon3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
            [topicon3 setImage:[UIImage imageNamed:@"top3.png"]];
            [cell addSubview:topicon3];
            [topicon3 release];
        }
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[TodayNewsTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        cell.titleLabel.text = model.title;
        cell.contentLabel.text = model.digest;
        NSNumber * a = model.replyCount;
        NSInteger b = [a integerValue];
        if (b > 10000) {
            CGFloat count = b * 1.0 / 10000 * 1.0;
            [cell.commentButton setTitle:[NSString stringWithFormat:@"  %.1f万跟帖", count] forState:UIControlStateNormal];
        }else{
            [cell.commentButton setTitle:[NSString stringWithFormat:@"  %@跟帖", model.replyCount] forState:UIControlStateNormal];
        }
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell release];
    return cell;
}

#pragma mark -
#pragma mark cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TodayNewsModel *model = [self.array objectAtIndex:indexPath.row +1];
    if ([model.skipType isEqualToString:@"special"]) {
        TodaySubjectViewController *subject = [[TodaySubjectViewController alloc]init];
        subject.skipID = model.skipID;
        [self.navigationController pushViewController:subject animated:YES];
        [subject release];
    }else{
        NewsDetailViewController *detail = [[NewsDetailViewController alloc]init];
        detail.docID = model.docid;
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
        
    }
    //    TodaySubjectViewController *subject = [[TodayNewsNextViewController alloc]init];
    //    [self.navigationController pushViewController:next animated:YES];
    //    [next release];
}

#pragma mark -
#pragma mark topView点击事件
- (void)topViewAction:(id)sender
{
//    ShowImageViewController *show = [[ShowImageViewController alloc]init];
//    [self.navigationController presentViewController:show animated:YES completion:nil];
//    [show release];
    TodayNewsModel *model = [self.array firstObject];
    if ([model.skipType isEqualToString:@"special"]) {
        TodaySubjectViewController *subject = [[TodaySubjectViewController alloc]init];
        subject.skipID = model.skipID;
        [self.navigationController pushViewController:subject animated:YES];
        [subject release];
    }else{
        NewsDetailViewController *detail = [[NewsDetailViewController alloc]init];
        detail.docID = model.docid;
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
        
    }

    
}
@end
